package com.personal.portal.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.TechTag;
import com.personal.portal.admin.mapper.TechTagMapper;
import com.personal.portal.admin.service.TechTagService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 技术标签服务实现类
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Service
public class TechTagServiceImpl implements TechTagService {

    @Resource
    private TechTagMapper techTagMapper;

    @Override
    public TechTag queryById(Long id) {
        validateId(id);
        return techTagMapper.selectOneById(id);
    }

    @Override
    public TechTag queryByEntity(TechTag techTag) {
        QueryWrapper queryWrapper = buildQueryWrapper(techTag);
        return techTagMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public List<TechTag> queryList(TechTag techTag) {
        QueryWrapper queryWrapper = buildQueryWrapper(techTag);
        return techTagMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public Result.PageResult<TechTag> queryPage(int pageNum, int pageSize, TechTag techTag) {
        QueryWrapper queryWrapper = buildQueryWrapper(techTag);
        Page<TechTag> paginate = techTagMapper.paginate(pageNum, pageSize, queryWrapper);
        return new Result.PageResult<>(paginate.getRecords(), paginate.getTotalRow(), pageNum, pageSize);
    }

    @Override
    public int insert(TechTag techTag) {
        validateTechTag(techTag);
        return techTagMapper.insert(techTag);
    }

    @Override
    public int update(TechTag techTag) {
        if (techTag.getId() == null) {
            throw new IllegalArgumentException("技术标签ID不能为空");
        }
        validateId(techTag.getId());
        validateTechTag(techTag);
        
        TechTag existing = techTagMapper.selectOneById(techTag.getId());
        if (existing == null) {
            throw new IllegalArgumentException("技术标签信息不存在");
        }
        
        return techTagMapper.update(techTag);
    }

    @Override
    public int deleteById(Long id) {
        validateId(id);
        
        TechTag existing = techTagMapper.selectOneById(id);
        if (existing == null) {
            throw new IllegalArgumentException("技术标签信息不存在");
        }
        
        return techTagMapper.deleteById(id);
    }

    @Override
    public List<Long> deleteBatch(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        List<Long> successIds = new ArrayList<>();
        List<Long> failedIds = new ArrayList<>();

        for (Long id : ids) {
            try {
                validateId(id);
                
                TechTag existing = techTagMapper.selectOneById(id);
                if (existing == null) {
                    failedIds.add(id);
                    continue;
                }

                int result = techTagMapper.deleteById(id);
                if (result > 0) {
                    successIds.add(id);
                } else {
                    failedIds.add(id);
                }
            } catch (Exception e) {
                failedIds.add(id);
            }
        }

        if (!successIds.isEmpty()) {
            return successIds;
        } else {
            throw new IllegalArgumentException("批量删除失败");
        }
    }

    /**
     * 验证技术标签信息
     */
    private void validateTechTag(TechTag techTag) {
        if (techTag == null) {
            throw new IllegalArgumentException("技术标签信息不能为空");
        }
        if (StringUtils.isBlank(techTag.getName())) {
            throw new IllegalArgumentException("标签名称不能为空");
        }
    }

    /**
     * 验证ID
     */
    private void validateId(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("无效的技术标签ID");
        }
    }

    /**
     * 构建查询条件
     */
    private QueryWrapper buildQueryWrapper(TechTag techTag) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TechTag::getId, techTag.getId(), techTag.getId() != null);
        queryWrapper.like(TechTag::getName, techTag.getName(), StringUtils.isNotEmpty(techTag.getName()));
        queryWrapper.like(TechTag::getDescription, techTag.getDescription(), StringUtils.isNotEmpty(techTag.getDescription()));
        queryWrapper.like(TechTag::getType, techTag.getType(), StringUtils.isNotEmpty(techTag.getType()));
        queryWrapper.eq(TechTag::getStatus, techTag.getStatus(), techTag.getStatus() != null);

        queryWrapper.orderBy(TechTag::getSortOrder, true);
        queryWrapper.orderBy(TechTag::getName, true);
        queryWrapper.orderBy(TechTag::getId, true);
        return queryWrapper;
    }
}